package tr.edu.yildiz.ce.dap.apm.util;

import java.io.File;

import tr.edu.yildiz.ce.dap.amc.services.AppAuthService;
import tr.edu.yildiz.ce.dap.amc.services.impl.AppAuthServiceImpl;
import tr.edu.yildiz.ce.dap.amc.util.AuthenticationFailedException;
import tr.edu.yildiz.ce.dap.amc.util.ManagerConnectionException;
import tr.edu.yildiz.ce.dap.apm.ProxyProperties;

public class AppManagerInitializer {

	public static void initApplications(){
		cleanTempFolder(ProxyProperties.getInstance().getProxyAutoApps());
		cleanTempFolder(ProxyProperties.getInstance().getProxyApps());
		AppAuthService appAuthService = new AppAuthServiceImpl();
		try {
			appAuthService.getApplications(	ProxyProperties.getInstance().getProxyApps(),
											ProxyProperties.getInstance().getProxyAutoApps(),
											ProxyProperties.getInstance().getWsUrl(),
											ProxyProperties.getInstance().getUsername(),
											ProxyProperties.getInstance().getPassword());
		} catch (ManagerConnectionException e) {
			e.printStackTrace();
		} catch (AuthenticationFailedException e) {
			e.printStackTrace();
		}
	}
	
	private static void cleanTempFolder(String folderPath){
		File folder = new File(folderPath);
		File[] files = folder.listFiles();
		for (File file : files) {
			if (!(file.getAbsolutePath().equals(ProxyProperties.getInstance().getProxyApps()) 
					|| file.getAbsolutePath().equals(ProxyProperties.getInstance().getProxyAutoApps())))
			file.delete();
		}
		
	}
	
}
